package vn.com.bookstore.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import vn.com.bookstore.dao.AccountDao;
import vn.com.bookstore.domain.model.Account;
import vn.com.bookstore.utils.DataHelper;

public class LoginService implements UserDetailsService{
	
	@Autowired
	private AccountDao accountDao;
	

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		List<GrantedAuthority> AUTHORITIES = new ArrayList<GrantedAuthority>();
		Account accountCustomer = accountDao.getUserByName(username, DataHelper.UserRole.ROLE_FOR_WEBSITE.getCode());
		if (accountCustomer != null) {
			AUTHORITIES.add(new SimpleGrantedAuthority(DataHelper.UserRole.ROLE_FOR_WEBSITE.getCode()));
			return new User(username, accountCustomer.getPassword(), true,
					true, true, true, AUTHORITIES);
		} else {
				throw new UsernameNotFoundException("User is authozization!!");
		}
	}
}
